package cc.lcdsg.shiro;

import cc.lcdsg.utils.MD5Util;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 001866
 * @date 2018/1/18.
 */
public class LoginFilter extends AuthorizingRealm {

    private static final String USER_NAME = "admin";
    private static final String PASSWORD = "123456";

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Set<String> rolenames = new HashSet<>();
        Set<String> promissions = new HashSet<>();
        rolenames.add("admin");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(rolenames);
        info.setStringPermissions(rolenames);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        if(token.getUsername().equals(USER_NAME)){
            return new SimpleAuthenticationInfo(USER_NAME, MD5Util.makeMD5(PASSWORD), getName());
        }else{
            throw new AuthenticationException();
        }
    }
}
